﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class DrainRecordReposity : Reposity
    {
        public List<Drain_system_Monthly_info> GetMonthlyDrainInfo(int stackId, float El, DateTime date)
        {
            var infos = from s in _dataContext.Drain_system_Monthly_infos
                                          where stackId == s.Stack_id && s.Date == date.Date && s.El == El
                                          orderby s.Drain_outlet_no ascending
                                          select s;

            if (infos != null && infos.Count() > 0)
            {
                return infos.ToList();
            }
            else
            {
                return null;
            }
        }

        public IDictionary<DateTime, stack_ardealite_ditch> QueryArdealiteInfo(int stackId, DateTime dateFrom, DateTime dateTo)
        {
            var ardealites = from s in _dataContext.stack_ardealite_ditches
                             where stackId == s.stack_id && s.date >= dateFrom && s.date < dateTo
                             select s;

            IDictionary<DateTime, stack_ardealite_ditch> infos = new Dictionary<DateTime, stack_ardealite_ditch>();
            foreach (stack_ardealite_ditch info in ardealites)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }

        public Drain_system_Monthly_info CreateDrainRecord(Drain_system_Monthly_info info)
        {
            _dataContext.Drain_system_Monthly_infos.InsertOnSubmit(info);
            SubmitChanges();
            return info;
        }

        public bool DeleteDrainInfos(int stackId, DateTime date)
        {
            var infos = from s in _dataContext.Drain_system_Monthly_infos
                        where stackId == s.Stack_id && s.Date == date.Date
                        select s;
            foreach (Drain_system_Monthly_info info in infos)
            {
                _dataContext.Drain_system_Monthly_infos.DeleteOnSubmit(info);
            }
            return true;
        }
    }
}